from general_neuro.conductance_neuron import Conductance_neuron
from general_neuro.alpha_synapse import Alpha_synapse
from scipy.integrate import odeint
from scipy import linspace,arange
from pylab import plot,show,subplot
import pylab

#----------------------------------------------------------------------------------
# some synaptic input
#----------------------------------------------------------------------------------
holding_V = -70.0 # in mV
c2 = Conductance_neuron(g_leak = 3.0, E_leak = -56.5, tau = 100, V_init = -56.5)

c2.register_input("E",Alpha_synapse(onset=14.5,tau=3.0,gmax=0.006,E_rev=10.0))
c2.register_input("I",Alpha_synapse(onset=11,tau=8.5,gmax=0.0175,E_rev=-79.0))
c2.set_V_clamp(holding_V) # set the neuron into V_clamp mode.

sv_init = c2.get_sv_init_list()

t = arange(0,200,0.1)
sv_results = odeint(c2.dX_dt, sv_init, t)

# reconstruct the various currents from the sv_results
I = {}
for name in c2.inputs.keys():
    I[name] = []
for i in range(len(t)):
    time = t[i]
    sv = sv_results[i]
    sv_dict = c2.sv_list_to_dict(sv)
    for name in c2.inputs.keys():
        I[name].append( -c2.inputs[name](sv_dict,time) )

num_currents = len(c2.inputs.keys())

plot_num = 0
for key in c2.inputs.keys():
    plot_num += 1
    subplot(num_currents,1,plot_num)
    if plot_num == 1:
        pylab.title("Voltage Clamp Simulation at %2.2f mV " % (holding_V))
    plot(t,I[key])
    name = "I_" + key
    pylab.ylabel(name)
show()

